﻿// ActionScript file
package org.nickro.project.web{
	import flash.external.ExternalInterface;
	import org.nickro.project.Config;
	
	public class BrowserUtil
	{
		/**
		 * 浏览器打开新窗口
		 * @param	url      String   新窗口的URL地址
		 * @param	width    Number	  新窗口的宽度 
		 * @param	height   Number   新窗口的高度
		 */
		public static function openNewWindow(url:String,width:Number=0,height:Number=0):void
		{
			if (width == 0 && height == 0) {
				if (jsAvailable()) ExternalInterface.call(JSScripts.OPEN_FULL_WINDOW, url);
				else jsUnavailable();
			}else {
				if (jsAvailable()) ExternalInterface.call(JSScripts.OPEN_NEW_WINDOW, url, width, height);
				else jsUnavailable();
			}
		}
		/**
		 * 浏览器打开邮件发送窗口
		 * @param	emailPath String  邮件发送地址
		 * @param	title	  String  邮件发送标题
		 * @param	body      String  邮件发送主体
		 * 
		 */
		public static function sendEmail(emailPath:String,title:String = null,body:String = null):void
		{
			if (jsAvailable()) ExternalInterface.call(JSScripts.SEND_EMAIL, emailPath, title, body);
			else jsUnavailable();
		}
		/**
		 *	@private	关闭浏览窗口
		 */
		public static function closeWindow():void
		{
			if (jsAvailable()) ExternalInterface.call(JSScripts.WINDOW_CLOSE);
			else jsUnavailable();
		}
		/**
		 * 浏览器窗口弹出提示窗，用于与浏览器的通讯测试,先推荐使用Console.log
		 * @param	params:测试传递参数
		 * 
		 */
		public static function jsTrace(params:Object):void
		{
			if (Config.debug) 
			{
				if (jsAvailable()) ExternalInterface.call(JSScripts.ALERT, params);
				else jsUnavailable();
			}
		}
		
		public static function jsAvailable():Boolean
		{
			return ExternalInterface.available;
		}
		
		public static function jsUnavailable():void
		{
			//show message like this:您禁用了javascript脚本，导致程序无法执行。
		}
	}
}

class JSScripts{
	public static var OPEN_NEW_WINDOW:XML = 
			<scripts><![CDATA[
			function(url,windowWidth,windowHeight){
				newWindow = window.open(url,'','width='+windowWidth+',height='+windowHeight+',top=0,left=0,toolbar=no,scrollbars=no,resizable=no,menubar=no,location=no, status=no');
			}
			]]></scripts>;
	
	public static var OPEN_FULL_WINDOW:XML = 
			<scripts><![CDATA[
			function(url,windowWidth,windowHeight){
				newWindow = window.open(url,'','width='+window.screen.width+',height='+window.screen.height+',top=0,left=0,toolbar=no,scrollbars=no,resizable=no,menubar=no,location=no, status=no');
			}
			]]></scripts>;
	
	public static var SEND_EMAIL:XML = 
			<scripts><![CDATA[
			function(emailPath,title,text){
				window.location.href = "mailto:"+emailPath+"";
				myform.action = "mailto:"+emailPath+"";
				var url = ""+emailPath+"?subject="+title+"&body="+text+"";
				document.write(url);
			}
			]]></scripts>
			
	public static var WINDOW_CLOSE:XML = 
			<scripts><![CDATA[
			function(){
				window.close();
			}
			]]></scripts>
			
	public static var ALERT:XML = 
			<scripts><![CDATA[
			function(params){
				alert(params);
			}
			]]></scripts>
}